// miniprogram/pages/purchase/list.js
var util = require("../../util/util.js")

var NullTip1 = {
  tipText: '亲，找不到您未完成任务',
    actionText: '请添加新任务',
  routeUrl: '../../pages/new/purchase'
}
var NullTip2 = {
  tipText: '亲，找不到您已完成任务',
    actionText: '请添加新任务',
  routeUrl: '../../pages/new/purchase'
}

var NullTip3 = {
  tipText: '亲，找不到您已完成任务',
  actionText: '请添加新任务',
  routeUrl: '../../pages/new/purchase'
}

var personNullTip = {
  tipText: '亲，找不到您的待审核采购计划',
  actionText: '添加',
  routeUrl: '../../pages/new/purchase'
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
      tasks: [ ],
          wait: [ {id:1,ptel:'青花瓷', ptitle:'青花瓷主歌部分',pCondition:5,date:'2021-5-6',content:"素胚勾勒出青花笔锋浓转"}
       , {
         id: 2,
         ptel: '兰亭序',
         ptitle: ' 兰亭序副歌部分', pCondition: 5, date: '2021 - 5 - 6 ', content: "兰亭临帖 行书如行云流水。月下门推 心细如你脚步碎"
         },

      ],
            onroad: [],
            inhouse:[],

            show: 'wait',
            nullTip: NullTip1
  },


   
  onLoad: function (options) {
 var that = this



     //获取token之后，拉取purchaseList表单列表
      console.log("purchase.list.js->onLoad()")
      if(getApp().globalData.token.length==0){ 
        console.log('本地不存在Token后')
        util.onlogin().then(this.getmylist)
    
      }else{
            var t = getApp().globalData.token
            // console.log(t)
            this.getmylist(t)
      }

 wx.stopPullDownRefresh()

  },

 /* 展开列表详情*/
 viewFilmDetail: function (e) {
    var that =this;
   var data = e.currentTarget.dataset
   console.log(that.data.wait[data.id])

   wx.redirectTo({
     url: "/pages/task/read?id="+data.id /* 详情*/
   })
 },

  /* 更改界面*/
  changeViewType: function(e) {
    var data = e.currentTarget.dataset
    this.setData({
      show: data.type,
      nullTip: data.type == 'wait' ? NullTip1 : (data.type=='onroad' ? NullTip2:NullTip3)
    })
  },


  getmylist: function(token){
    var that = this
    wx.login({
          success(res) {
             if (res.code) {
                  console.log("本地token:" + token)
                  wx.request({

                    url: 'https://2000022758.zhangqx.com/getList',
                    // method:'POST',
                    data: {
                      token: token,
                    },
                    success(res) {
                      console.log(res.data.data)
                      that.setData({
                        tasks: res.data.data
                      })
                      //that.data.tasks = res.data.data
                    }
                  })
              }
          }
        }) 
  },

  printError: function (res) {
      console.log("返回的信息为：" + res)
    },

       task() {
         console.log(this.data.tasks[0].ptitle)
         // console.log(this.data.tasks.length)
       },

    fetchPurchaseList: function (cb) {
      var that = this;
      wx.request({
        url: 'https://2000022758.zhangqx.com/fetchPurchase',
        data: {
          //Start: start, //本地存储的积累指针
          //Count: count, //服务器数据库的控制‘拉取数量’
        },
        header: {
          "Content-Type": "application/json,application/json"
        },
        success(res) {
          // console.log(">> 拉取任务列: " + res.data.data.length)

          if (res.data.data.length === 0) {
            that.setData({
              hasMore: false,
            })
          } else {
            that.setData({
              tasks: that.data.tasks.concat(res.data.data),
              start: that.data.start + res.data.data.length,
              //showLoading: false,
            })
            console.log('start' + that.data.start)
          } // end ifElse

          wx.stopPullDownRefresh()
          typeof cb == 'function' && cb(res.data.data)

        } // end success
        ,
      }) // end of request
    } // end of fetch
    ,
    LoadHistory: function (options) {
      let promise = new Promise((resolve, reject) => {
        var that = this
        this.fetchPurchaseList(
          function (list) {
            console.log('---获取任务数据库之后')
            var date = util.getDate()
            var time = util.getTime()
            var now_data = []
            var task_history
            wx.getStorage({
              key: 'task_history', //从本地存储中获取 过去存储的任务历史

              success: function (res) {
                now_data = res.data
                console.log('----获取缓存----')
                //console.log(task_history)

              }
            })
            //平衡本地存储的信息，与服务器上的数据

            if (now_data.length == 0) {
              console.log('----为空插入----')

              task_history = now_data.concat(list)
              console.log(task_history)
            }


            //四、将修改好的数据插入本地缓存中
            wx.setStorage({
              key: 'task_history',
              data: task_history,
              success: function (res) {
                //console.log(res)
                console.log('----任务历史本地存储成功----')
                that.data.tasks = task_history
                if (that.data.tasks.length >= 1) {
                  resolve("成功加载历史数据")
                } else reject('失败le ')
              }
            })

          } //end of function
        ) //end od fetch
      })
      return promise




    },

  
})